<?php
namespace Forxx\Api;

use \Lavender\Errno;
use \Lavender\Exception;

class Lbs
{
	//查找的最远距离（单位：公里）
	public static $distance = 1000;

	//可查询的性别值
	public static $query_sex = array(
		'female' => 0,
		'male' => 1,
		'all' => 2,
	);


	public static $query_type = array(
		'user' => 2,
		'vmt' => 1,
	);
	
	/**
	 * 附近的公众号(推荐公众号)
	 * @param float $lon
	 * @param float $lat
	 * @param float $type 
	 * @param array $options = array(
	 * 		distance = int(千米)
	 * 		offset = int(页码)
	 * 		length = int(页码长度)
	 * );
	 */
	public static function nearby_public($lon, $lat, $type, array $options = array())
	{
		$offset = (intval($options['offset']) < 1) ? 1 : intval($options['offset']);
		$length = (intval($options['length']) < 1) ? 50 : intval($options['length']);
		$distance = (intval($options['distance']) < 1) ? 50 : intval($options['distance']);
		
		$condition = array(
			array('$geoNear' => array(
				'near' => array(floatval($lon), floatval($lat)),
				'distanceField' => 'dis',
				'spherical' => true,
				'distanceMultiplier' => 6371,
				'maxDistance' => $distance/6371, //$distance,
				'num' => $offset * $length
			)),
			array('$skip' => ($offset -1) * $length),
		);
		
		//维修企业
		$loc = array();
		if($type == GoloPublicAccount::$public_type['repair_shop']){
			$loc = Dao\RepairShopTable::instance()->aggregate($condition);
		}
		//停车场
		if($type == GoloPublicAccount::$public_type['car_park']){
			$loc = Dao\CarParkTable::instance()->aggregate($condition);
		}
		if(empty($loc)){
			return null;
		}
		
		$ids = array();
		$loc_data = array();
		foreach($loc['result'] as $value){
			if(is_integer($value['id'])){
				$ids[] = $value['id'];
				$loc_data[$value['id']] = $value;
			}
		}
		
		$loc['result'] = array();
		$loc['result'] = $loc_data;
		return $loc;
	}
	
	/**
	 * 同城推荐
	 * @param float $lon
	 * @param float $lat
	 * @param array $options = array(
	 * 		type = int(类型:1技师，2车友)
	 * 		distance = int(千米)
	 * 		status = int(在线状态)
	 * 		offset = int(页码)
	 * 		length = int(页码长度)
	 * );
	 */
	public static function recommend_lbs($lon, $lat, array $options = array())
	{
		$offset = (intval($options['offset']) < 1) ? 1 : intval($options['offset']);
		$length = (intval($options['length']) < 1) ? 100 : intval($options['length']);
		$distance = (intval($options['distance']) < 1) ? self::$distance : intval($options['distance']);
		$condition = array(
			array('$geoNear' => array(
				'near' => array(floatval($lon), floatval($lat)),
				'distanceField' => 'dis',
				'spherical' => true,
				'distanceMultiplier' => 6371,
				'maxDistance' => $distance/6371, //$distance,
				'num' => $offset * $length
			)),
			array('$skip' => ($offset -1) * $length),
		);
		//附近的技师
		if($options['type'] == 1){
			return Dao\LbsVmtTable::instance()->aggregate($condition, true);
		}
		//附近的用户
		if($options['type'] == 2){
			return Dao\LbsTable::instance()->aggregate($condition, true);
		}
		
		return null;
	}
	
	/**
	 * 附近的人 （新）
	 * 
	 * @param float $lon
	 * @param float $lat
	 * @param array $param
	 */
	public static function nearby_people($lon, $lat, $param)
	{
		$condition = array(
			array('$geoNear' =>
				array(
					'near' => array($lon, $lat),
					'distanceField' => 'dis',
					'spherical' => true,
					'distanceMultiplier' => 6371,
					'maxDistance' => intval($param['dis'])/6371,
					'num' => $param['offset'] * $param['length'])),
			array('$skip' => ($param['offset'] -1) * $param['length']),
			array('$match' => array('sex' => $param['sex'])),
		);
		
		if($param['type'] == User::$user_roles['vmt']){
			$loc = Dao\LbsVmtTable::instance()->aggregate($condition, true);
		}elseif($param['type'] == User::$user_roles['normal']){
			$loc = Dao\LbsTable::instance()->aggregate($condition, true);
		}else{
			return null;
		}
		if($loc['ok'] != 1){
			return null;
		}
		return $loc['result'];
	}
	
	/**
	 * 获取附近的人的信息
	 * @param  int $uid    用户id
	 * @param  float $lon    经度
	 * @param  float $lat    纬度
	 * @param  int $sex    性别
	 * @param  int $offset 页码
	 * @param  int $len    页长
	 * @return array         
	 */
	public static function get_nearby($uid, $lon, $lat, $sex, $offset, $len, $type, $status, $distance)
	{
		if(!in_array($sex, self::$query_sex)){
			throw new Exception("param sex invalid", Errno::PARAM_INVALID);
		}
		
		// $condition = array(
		//     'coordinate' => array(
		//         '$near' => array($lon, $lat),
		//         '$maxDistance' => self::$distance,
		//     )
		// );
		// $loc = LbsTable::instance()->get($condition, array(), '', $offset, $len);
		// $condition = array(
		//     'geoNear' => LbsTable::get_collection_name(),
		//     'near' => array($lon, $lat),
		//     'spherical' => true,
		//     'num' => 5,
		//     'maxDistance' => self::$distance / 6371,
		// );
		// $loc = LbsTable::instance()->command($condition);
		//搜索条件
		$offset = ($offset < 1) ? 1 : intval($offset);
		$condition = array(
			array('$geoNear' => array(
				'near' => array($lon, $lat),
				'distanceField' => 'dis',
				'spherical' => true,
				'distanceMultiplier' => 6371,
				'maxDistance' => intval($distance)/6371, //$distance,
				'num' => $offset * $len
			)),
			array('$skip' => ($offset -1) * $len),
		);

		//筛选性别
		if($sex != self::$query_sex['all']){
			$condition[] = array('$match' => array('sex' => $sex));
		}

		$is_vmt = false;
		//筛选技师
		if($type == \Golo\Api\User::$user_roles['vmt']){
			$is_vmt = true;
			//获取附近的技师
			$loc = Dao\LbsVmtTable::instance()->aggregate($condition, true);
		}else{
			//获取附近的人
			$loc = Dao\LbsTable::instance()->aggregate($condition, true);
		}

		if($loc['ok'] != 1){
			return array();
		}
		//附近的人的ID
		$uids = self::searchMultiArray($loc['result'], 'uid');
		
		//附近的人的状态（空闲、忙碌、离线）
		if($status != 0){
			$user_status = User::get_status($uids);
			switch ($status){
				case UserWrite::$user_status['idle'] :
					$user_status = array_filter($user_status, function($var){
						if($var != UserWrite::$user_status['idle']){
							return false;
						}
						return true;
					});
					break;
				
				case UserWrite::$user_status['busy']:
					$user_status = array_filter($user_status,function($var){
						if($var != UserWrite::$user_status['busy']){
							return false;
						}
						return true;
					});
					break;
				
				case UserWrite::$user_status['offline']:
					$user_status = array_filter($user_status,function($var){
						if($var != UserWrite::$user_status['offline']){
							return false;
						}
						return true;
					});
					break;
			}
			$uids = array_keys($user_status);
		}
		
		//附近的人的基本信息
		$fields = array('user_id', 'user_name', 'nick_name', 'email', 'mobile', 'signature', 'set_face_time', 'roles');
		$user = User::get_bases($uids, $fields);
		$result = array();
		//拼装附近的人的信息
		$friend = array();
		if(!empty($uids)){
			$user_logos=MineCar::get_auto_logos($uids);
			
			$address = Area::get_user_address_list($uids);
			$friend = Dao\ContactTable::instance()->get($uid);
			$user_exts = $address['user_exts'];
			$ns = $address['nations'];
			$ps = $address['provinces'];
			$cs = $address['citys'];
		}
		
		foreach ($loc['result'] as $key => $value) {
			if(!isset($user[$value['uid']])){
				return array_values($result);
			}
			//过滤掉自己
			if($value['uid'] == $uid){
				continue;
			}
			
			
			//如果昵称为空，则用户名等于昵称
			if(empty($user[$value['uid']]['nick_name'])){
				$user[$value['uid']]['nick_name'] = $user[$value['uid']]['user_name'];
			}
			unset($user[$value['uid']]['user_name']);
			
			$result[$value['uid']] = $user[$value['uid']];
			$result[$value['uid']]['uid'] = $value['uid'];
			$result[$value['uid']]['sex'] = $value['sex'];
			
			$u = User::get_base($value['uid'], $fields)[0];
			$result[$value['uid']]['is_friend'] = 0;
			if(!empty($friend)){
				$index = array_search($u['user_id'], $friend);
				if(is_integer($index)){
					array_splice($friend, $index, 1);
					$result[$value['uid']]['is_friend'] = 1;
				}
			}
			$result[$value['uid']]['user_name'] = $u['user_name'];
			$result[$value['uid']]['email'] = $u['email'];
			$result[$value['uid']]['mobile'] = $u['mobile'];
			$result[$value['uid']]['roles'] = $u['roles'];
			
			$result[$value['uid']]['distance'] = $value['dis'] * 1000;
			$result[$value['uid']]['lon'] = $value['coordinate']['longitude'];
			$result[$value['uid']]['lat'] = $value['coordinate']['latitude'];
			
						
			if($is_vmt){
				$flag=1;//用户身份, flag字段   0未知, 1普通用户, 2是技师不是专家, 3专家申请中, 4是专家, 5专家申请被拒绝
				$expert_info=Dao\PublicExpertTable::instance()->get($value['uid'],array('status'))[0];
				if(!isset($expert_info)||is_null($expert_info))
				{
					$flag=2;//是技师，不是专家
				}
				else
				{
					$status =isset($expert_info['status'])?$expert_info['status']:"";
					if($status==0){
						$flag=3;//专家申请中
					}
					if($status==1){
						$flag=4;//是专家
					}
					if($status==2){
						$flag=5;//申请专家被拒绝
					}
				}
				$result[$value['uid']]['flag'] = $flag;
			}
			
			//加入车标
// 			$car_logo=MineCar::get_auto_logos(array($value['uid']))[$value['uid']];
			$car_logo = $user_logos[$value['uid']];
			foreach($car_logo as $logo_url){
				if($logo_url['is_bind']){
					$car_logo_url[] = $logo_url['logo_url'];
					$auto_code[] = $logo_url['auto_code'];
					$car_series_name[] = $logo_url['car_series_name'];
				}
			}
			$result[$value['uid']]['car_logo_url'] = $car_logo_url;
			$result[$value['uid']]['auto_code'] = $auto_code;
			$result[$value['uid']]['car_series_name'] = $car_series_name;
			
			$result[$value['uid']]['country']=$ns[$user_exts[$value['uid']]['country']];
			$result[$value['uid']]['province']=$ps[$user_exts[$value['uid']]['province']];
			$result[$value['uid']]['city']=$cs[$user_exts[$value['uid']]['city']];
			
			unset($car_logo_url);
			unset($auto_code);
			unset($car_series_name);
		}
		
		return array_values($result);
	}
	
	
	
	
	/**
	 * 获取附近的维修厂的信息
	 * @param  float $lon    经度
	 * @param  float $lat    纬度
	 * @param  int $offset 页码
	 * @param  int $len    页长
	 * @return array         
	 */
	public static function get_neary_repair_shop($lon, $lat, $offset, $len, $distance)
	{
		//搜索条件
		$limit = $offset + $len;
		$condition = array(
			array('$geoNear' => array(
				'near' => array($lon, $lat),
				'distanceField' => 'dis',
				'spherical' => true,
				'distanceMultiplier' => 6371,
				'maxDistance' => intval($distance)/6371, //$distance
				'num' => $limit
			)),
			array('$skip' => $offset),
		);
		//获取附近的维修厂的地理位置
		$loc = Dao\RepairShopTable::instance()->aggregate($condition);
		if($loc['ok'] != 1){
			return array();
		}
		$ids = array();
		$loc_data = array();
		foreach($loc['result'] as $value){
			if(is_integer($value['id'])){
				$ids[] = $value['id'];
				$loc_data[$value['id']] = $value;
			}
		}
		$pbase = array();		
		$pbase = Dao\PublicBaseTable::instance()->get($ids,array('id','company_name','credit_level'));
		if(empty($pbase)){
			return null;
		}
		$pid = array();
		$p_data = array();
		foreach($pbase as $p){
			$pid[] = $p['id'];
			$p_data[$p['id']] = $p;
		}
		Dao\PublicFactoryTable::instance()->set_first_key('pub_id');
		$fbase = array();
		$fbase = Dao\PublicFactoryTable::instance()->get($pid,array('pub_id','address'), array('sup_id' => 0));
		foreach($fbase as $f){
			$p_data[$f['pub_id']]['address'] = $f['address'];
		}
		$ubase = User::get_base($pid,array('user_id','set_face_time','signature','reg_zone'));
		if(!empty($ubase)){
			foreach($ubase as $u){
				$_data['id'] = $u['user_id'];
				$_data['distance'] = $loc_data[$u['user_id']]['dis'] * 1000;
				$_data['name'] = $p_data[$u['user_id']]['company_name'];
				$_data['lon'] = $loc_data[$u['user_id']]['coordinate']['lon'];
				$_data['lat'] = $loc_data[$u['user_id']]['coordinate']['lat'];
				$_data['credit_level'] = $p_data[$u['user_id']]['credit_level'];
				$_data['address'] = is_null($p_data[$u['user_id']]['address']) ? '' : $p_data[$u['user_id']]['address'];
				$_data['signature'] = $u['signature'];
				$_data['face'] = User::get_face($u['user_id'],$u['set_face_time'],$u['reg_zone'])['thumb'];
				$result[] = $_data;
			}
		}
		return $result;
	}
	
	/**
	 * 获取附近的停车场厂的信息
	 * @param  float $lon    经度
	 * @param  float $lat    纬度
	 * @param  int $offset 页码
	 * @param  int $len    页长
	 * @return array
	 */
	public static function get_neary_car_park($lon, $lat, $offset, $len, $distance)
	{
		//搜索条件
		$limit = $offset + $len;
		$condition = array(
			array('$geoNear' => array(
				'near' => array($lon, $lat),
				'distanceField' => 'dis',
				'spherical' => true,
				'distanceMultiplier' => 6371,
				'maxDistance' => intval($distance)/6371, //$distance
				'num' => $limit
			)),
			array('$skip' => $offset),
		);
		//获取附近的停车厂的地理位置
		$loc = Dao\CarParkTable::instance()->aggregate($condition);
		if($loc['ok'] != 1){
			return array();
		}
	
		//拼装附近的停车场的信息
		foreach($loc['result'] as $key => $value){
			$ids[] = $value['id'];
			$loc_data[$value['id']] = $value;
		}
		
		$pbase = Dao\PublicBaseTable::instance()->get($ids,array('id','company_name','credit_level'));
		if(empty($pbase)){
			return null;
		}
		foreach($pbase as $p){
			$pid[] = $p['id'];
			$p_data[$p['id']] = $p;
		}
		Dao\PublicFactoryTable::instance()->set_first_key('pub_id');
		$fbase = Dao\PublicFactoryTable::instance()->get($pid,array('pub_id','address'));
		foreach($fbase as $f){
			$f_data[$f['pub_id']] = $f['address'];
		}
		$ubase = User::get_base($pid,array('user_id','set_face_time','signature','reg_zone'));
		if(!empty($ubase)){
			foreach($ubase as $u){
				$_data['id'] = $u['user_id'];
				$_data['distance'] = $loc_data[$u['user_id']]['dis'] * 1000;
				$_data['name'] = $p_data[$u['user_id']]['company_name'];
				$_data['lon'] = $loc_data[$u['user_id']]['coordinate']['lon'];
				$_data['lat'] = $loc_data[$u['user_id']]['coordinate']['lat'];
				$_data['credit_level'] = $p_data[$u['user_id']]['credit_level'];
				$_data['address'] = $f_data[$u['user_id']];
				$_data['signature'] = $u['signature'];
				$_data['face'] = User::get_face($u['user_id'],$u['set_face_time'],$u['reg_zone'])['thumb'];
		
				$result[] = $_data;
			}
		}
		
		return $result;
	}

	/**
	 * 获取数组某一个键的所有值
	 */
	private static function searchMultiArray(array $array, $search, $mode = 'key') {
		$res = array();
		foreach (new \RecursiveIteratorIterator(new \RecursiveArrayIterator($array)) as $key => $value) {
			if($search !== ${${"mode"}}){
				continue;
			}
			if($mode == 'key'){
				$res[] = $value;
			}else{
				$res[] = $key;
			}
		}
		return $res;
	}

	/**
	 * 更新用户的位置信息
	 * @param  int $uid     用户id
	 * @param  array $content 更新的内容
	 */         
	public static function update_location($uid, $content, $is_vmt = false, $optional = array())
	{
		if(!is_array($optional)){
			throw new Exception("optional invalid", Errno::PARAM_INVALID);
			
		}
		
		//判断用户是否开启通过位置查找到我，如果没开启，则不用更新。
		$fields = array('find_by_local');
		$result = User::get_conf(array($uid), $fields)[0];
		if(!empty($result) && $result['find_by_local'] == 0){//默认开启, 1开启，0关闭
			return true;
		}
		
		//搜索条件
		$condition = array('uid' => intval($uid));
		
		//更新用户的地理位置
		if($is_vmt){
			$vmt = Dao\LbsVmtTable::instance()->update($condition, $content, true, $optional);
			if(!$vmt){
				return false;
			}
		}
		
		$normal = Dao\LbsTable::instance()->update($condition, $content, true, $optional);
		if(!$normal){
			return false;
		}
		return true;
	}
	
	/**
	 * 更新用户的地理位置 | 技师 | 医生
	 * @param int $uid
	 * @param array $content = array(
	 * 							'uid' => $user_id,
	 * 							'coordinate' => array(
	 * 								'longitude' => $lon,
	 * 								'latitude' => $lat);
	 * 							'sex' => 0,
	 * 							'time' => $time;
	 * @param int $user_roles
	 * @param int $optional
	 * @throws Exception
	 * @return number
	 */
	public static function update_user($uid, $content, $user_roles, $optional = array())
	{
		if(!is_array($optional)){
			throw new Exception("optional invalid", Errno::PARAM_INVALID);
		}
		
		$condition = array('uid' => intval($uid));
		
		$res = Dao\LbsTable::instance()->update($condition, $content, true, $optional);
		if($res && ($user_roles & User::$user_roles['vmt'])){
			return Dao\LbsVmtTable::instance()->update($condition, $content, true, $optional);
		}
		
		return $res;
	}
	
	//获取指定用户的loc
	public static function user_loc($uid)
	{
		if(is_array($uid)){
			$condition = array('uid' => array('$in' => $uid));
		}else{
			$condition = array('uid' => intval($uid));
		}
		
		return Dao\LbsTable::instance()->get($condition);
	}

	/**
	 * 设置修理厂位置信息
	 * @param  int $public_id     公众号id
	 * @param  array $content 更新的内容 eg. 
	 * array("id" => 10000, "coordinate" => array("lon" => 111.111, "lat" => 22.222), "name" => 'xxxxx', 'bf_id' => 222)
	 * 
	 */         
	public static function set_repairshop_loc($public_id, $content, $optional = array())
	{
		if(!is_array($optional)){
			throw new Exception("optional invalid", Errno::PARAM_INVALID);
			
		}
		//搜索条件
		$condition = array('id' => intval($public_id));
		
		//更新地理位置
		return Dao\RepairShopTable::instance()->update($condition, $content, false, array('upsert' => true));
	}
	
	/**
	 * 设置公众号的位置信息
	 * @param  int $public_id     公众号id
	 * @param  array $content 更新的内容 eg.
	 * array("id" => 10000, "coordinate" => array("lon" => 111.111, "lat" => 22.222), "name" => 'xxxxx', 'bf_id' => 222)
	 *
	 */
	public static function set_public_loc($public_id, $content, $optional = array())
	{
		if(!is_array($optional)){
			throw new Exception("optional invalid", Errno::PARAM_INVALID);
		}
		
		//搜索条件
		$condition = array('id' => intval($public_id));
		if(is_numeric($content['id']) && !is_integer($content['id'])){
			$content['id'] = intval($content['id']);
		}
		$type_public = array_flip(GoloPublicAccount::$public_type);
		if(!array_key_exists($optional['type'],$type_public)){
			throw new Exception("optional type invalid", Errno::PARAM_INVALID);
		}else{
			//更新地理位置
			$table = "\\Golo\\Api\\Dao\\".GoloPublicAccount::$lbs_table[$type_public[$optional['type']]];
			if($table == null){
				throw new Exception("optional type of table invalid", Errno::PARAM_INVALID);
			}
			
			return $table::instance()->update($condition, $content, false, array('upsert' => true));
		}
	}
	
	//清除公众号的地理位置
	public static function clear_public_loc($public_id, $type = 2)
	{
		if(empty($public_id)){
			throw new \Exception('public is empty');
		}
		$type_public = array_flip(GoloPublicAccount::$public_type);
		$table = "\\Golo\\Api\\Dao\\".GoloPublicAccount::$lbs_table[$type_public[$type]];
		
		return $table::instance()->delete(array('id' => $public_id), true);
	}

	/**
	 * 清除地理位置
	 * @param  int $uid 用户id
	 */
	public static function clear_location($uid, $is_vmt = false)
	{
		$condition = array('uid' => intval($uid));
		
		if($is_vmt){
			$vmt = Dao\LbsVmtTable::instance()->delete($condition, true);
			if(!$vmt){
				return false;
			}
		}
		return Dao\LbsTable::instance()->delete($condition, true);
	}
	
	/**
	 * 清除维修厂的地理位置
	 * @param  int 
	 */
	public static function clear_repairshop_loc($repairshop_id)
	{
		$condition = array('id' => $repairshop_id);
	
		return Dao\RepairShopTable::instance()->delete($condition, true);
	}
	
	public static function get_user($uid, $lon, $lat, $sex, $offset, $len, $type, $status, $distance)
	{
		if(!in_array($sex, self::$query_sex)){
			throw new Exception("param sex invalid", Errno::PARAM_INVALID);
		}
		
		// $condition = array(
		//     'coordinate' => array(
		//         '$near' => array($lon, $lat),
		//         '$maxDistance' => self::$distance,
		//     )
		// );
		// $loc = LbsTable::instance()->get($condition, array(), '', $offset, $len);
		// $condition = array(
		//     'geoNear' => LbsTable::get_collection_name(),
		//     'near' => array($lon, $lat),
		//     'spherical' => true,
		//     'num' => 5,
		//     'maxDistance' => self::$distance / 6371,
		// );
		// $loc = LbsTable::instance()->command($condition);
		//搜索条件
		$offset = ($offset < 1) ? 1 : intval($offset);
		$condition = array(
			array('$geoNear' => 
				array(
					'near' => array($lon, $lat),
					'distanceField' => 'dis',
					'spherical' => true,
					'distanceMultiplier' => 6371,
					'maxDistance' => intval($distance)/6371, //$distance,
					'num' => $offset * $len
				)
			),
			array('$skip' => ($offset -1) * $len),
		);


		if($loc['ok'] != 1){
			return array();
		}
		//附近的人的ID
		$uids = self::searchMultiArray($loc['result'], 'uid');
		
		//附近的人的状态（空闲、忙碌、离线）
		if($status != 0){
			$user_status = User::get_status($uids);
			switch ($status){
				case UserWrite::$user_status['idle'] :
					$user_status = array_filter($user_status, function($var){
						if($var != UserWrite::$user_status['idle']){
							return false;
						}
						return true;
					});
					break;
				
				case UserWrite::$user_status['busy']:
					$user_status = array_filter($user_status,function($var){
						if($var != UserWrite::$user_status['busy']){
							return false;
						}
						return true;
					});
					break;
				
				case UserWrite::$user_status['offline']:
					$user_status = array_filter($user_status,function($var){
						if($var != UserWrite::$user_status['offline']){
							return false;
						}
						return true;
					});
					break;
			}
			$uids = array_keys($user_status);
		}
		
		//附近的人的基本信息
		$fields = array('user_id', 'user_name', 'nick_name', 'email', 'mobile', 'signature', 'set_face_time', 'roles');
		$user = User::get_bases($uids, $fields);
		$result = array();
		//拼装附近的人的信息
		
		if(!empty($uids)){
			$user_logos=MineCar::get_auto_logos($uids);
		}
		foreach ($loc['result'] as $key => $value) {
			if(!isset($user[$value['uid']])){
				return array_values($result);
			}
			//过滤掉自己
			if($value['uid'] == $uid){
				continue;
			}
			
			//如果昵称为空，则用户名等于昵称
			if(empty($user[$value['uid']]['nick_name'])){
				$user[$value['uid']]['nick_name'] = $user[$value['uid']]['user_name'];
			}
			unset($user[$value['uid']]['user_name']);
			
			$result[$value['uid']] = $user[$value['uid']];
			$result[$value['uid']]['uid'] = $value['uid'];
			$result[$value['uid']]['sex'] = $value['sex'];
			
			$u = User::get_base($value['uid'], $fields)[0];
			$result[$value['uid']]['user_name'] = $u['user_name'];
			$result[$value['uid']]['email'] = $u['email'];
			$result[$value['uid']]['mobile'] = $u['mobile'];
			$result[$value['uid']]['roles'] = $u['roles'];
			
			$result[$value['uid']]['distance'] = $value['dis'] * 1000;
			$result[$value['uid']]['lon'] = $value['coordinate']['longitude'];
			$result[$value['uid']]['lat'] = $value['coordinate']['latitude'];
			
						
			if($is_vmt){
				$flag=1;//用户身份, flag字段   0未知, 1普通用户, 2是技师不是专家, 3专家申请中, 4是专家, 5专家申请被拒绝
				$expert_info=Dao\PublicExpertTable::instance()->get($value['uid'],array('status'))[0];
				if(!isset($expert_info)||is_null($expert_info))
				{
					$flag=2;//是技师，不是专家
				}
				else
				{
					$status =isset($expert_info['status'])?$expert_info['status']:"";
					if($status==0){
						$flag=3;//专家申请中
					}
					if($status==1){
						$flag=4;//是专家
					}
					if($status==2){
						$flag=5;//申请专家被拒绝
					}
				}
				$result[$value['uid']]['flag'] = $flag;
			}
			
			//加入车标
// 			$car_logo=MineCar::get_auto_logos(array($value['uid']))[$value['uid']];
			$car_logo = $user_logos[$value['uid']];
			foreach($car_logo as $logo_url){
				if($logo_url['is_bind']){
					$car_logo_url[] = $logo_url['logo_url'];
					$auto_code[] = $logo_url['auto_code'];
					$car_series_name[] = $logo_url['car_series_name'];
				}
			}
			$result[$value['uid']]['car_logo_url'] = $car_logo_url;
			$result[$value['uid']]['auto_code'] = $auto_code;
			$result[$value['uid']]['car_series_name'] = $car_series_name;
			unset($car_logo_url);
			unset($auto_code);
			unset($car_series_name);
		}
		
		return array_values($result);
	}
	
}
